package com.bestcem.xm.user.controller;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.AuthErrorCodeEnum;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.user.controller.vo.user.WeChatCodeResultVO;
import com.bestcem.xm.user.controller.vo.user.request.WeChatCodeTokenVO;
import com.bestcem.xm.user.controller.vo.user.request.WeChatTokenRequestVO;
import com.bestcem.xm.user.service.UserWxWorkService;
import com.bestcem.xm.user.service.dto.user.WeChatCodeResultDTO;
import com.bestcem.xm.user.service.dto.user.WeChatCodeTokenDTO;
import com.bestcem.xm.user.service.dto.user.WeChatTokenDTO;
import com.bestcem.xm.user.service.dto.wechatwork.WechatWorkUnbindDTO;
import com.bestcem.xm.user.util.convert.UserConvert;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 企业微信相关服务
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/18 11:25
 */
@RestController
@RequestMapping(value = "/authorize/wechatwork")
public class UserWxWorkController {

    @Resource
    private UserWxWorkService userWxWorkService;

    @Resource
    private UserConvert userConvert;


    /**
     * 企业微信登陆 手机号码和邮箱登录 ok （废弃，v2替代）
     *
     * @param requestVO 登陆信息
     * @param response
     * @return com.bestcem.xm.util.XmResultJson<java.lang.Object>
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/04/19 19:30
     */
    @AuthIgnore
    @PostMapping("/v1/login/")
    public XmResultJson<Object> wechatTokenView(@RequestBody @Validated WeChatTokenRequestVO requestVO, HttpServletRequest request, HttpServletResponse response) {
        WeChatTokenDTO weChatTokenDTO = userConvert.wechatTokenVO2DTO(requestVO);
        weChatTokenDTO.setIp(request.getRemoteAddr());
        ServiceResult<Object> result = userWxWorkService.tokenView(weChatTokenDTO, response);
        if (!result.isSuccess()) {
            return XmResultJson.fail(result.getErrorData(), result.getMsg());
        }
        return XmResultJson.success(result.getData());
    }


    /**
     * 企业微信进行公司代号及账户密码登录 ok
     * apps_mysql/wechatwork/handlers.py:851
     *
     * @param tokenVO 登陆信息
     * @return com.bestcem.xm.util.XmResultJson<java.lang.Object>
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/04/19 19:31
     */
    @AuthIgnore
    @PostMapping("/v2/login/")
    public XmResultJson<WeChatCodeResultVO> weChatCodeTokenView(@RequestBody @Validated WeChatCodeTokenVO tokenVO, HttpServletRequest request) {
        WeChatCodeTokenDTO codeToken = userConvert.wechatCodeTokenVO2DTO(tokenVO);
        ServiceResult<WeChatCodeResultDTO> result = userWxWorkService.weChatCodeTokenView(codeToken, request);
        if (!result.isSuccess()) {
            return XmResultJson.fail(result.getErrorData(), result.getMsg());
        }
        WeChatCodeResultVO resultVO = userConvert.wechatCodeTokenDTO2CVO(result.getData());
        return XmResultJson.success(resultVO);
    }


    /**
     * 解除企业微信绑定
     *
     * @param id 用户id
     * @return 解绑的结果
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/7/18 15:06
     */
    @PutMapping("/wxwork/{id}/unbind/")
    public XmResultJson<WechatWorkUnbindDTO> unbind(@PathVariable("id") String id) {
        String userId = SecurityContextHolder.getToken().getUserId();
        if (ObjectUtils.isEmpty(id) && !id.equals(userId)) {
            return XmResultJson.fail(AuthErrorCodeEnum.USER_NOT_EXISTS, "user not found");
        }

        ServiceResult<WechatWorkUnbindDTO> serviceResult = userWxWorkService.unbindUser(userId);
        if (!serviceResult.isSuccess()) {
            return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
        }
        return XmResultJson.success(serviceResult.getData());
    }
}
